package com.hzw.saas.common.util;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class HttpContextUtils {

    public static final String HTTP_SCHEMA = "http";
    public static final String HTTPS_SCHEMA = "https";
    public static final String SCHEMA_SUFFIX = "://";

    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static String getDomain() {
        HttpServletRequest request = getHttpServletRequest();
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
    }

    public static String getOrigin() {
        HttpServletRequest request = getHttpServletRequest();
        return request.getHeader("Origin");
    }

    /**
     * 文件下载时用于写文件头部信息
     *
     * @param request  http请求
     * @param response http响应
     * @param fileName 文件名
     */
    public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName, long fileLength) throws UnsupportedEncodingException {
        String agent = request.getHeader("USER-AGENT");
        String encodedFileName = null;
        if (null != agent && agent.contains("Mozilla")) {
            encodedFileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        } else {
            encodedFileName = URLEncoder.encode(fileName, "UTF-8");
        }
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"");
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        if (fileLength > 0) {
            response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileLength));
        }
    }

    public static void setImageHeader(HttpServletRequest request, HttpServletResponse response, long fileLength, String suffix)throws UnsupportedEncodingException {
        String mediaType;
        switch (suffix) {
            case "gif":
                mediaType = MediaType.IMAGE_GIF_VALUE;
                break;
            case "png":
                mediaType = MediaType.IMAGE_PNG_VALUE;
                break;
            default:
                mediaType = MediaType.IMAGE_JPEG_VALUE;
        }

        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"inline");
        response.setHeader(HttpHeaders.CONTENT_TYPE, mediaType);
        if (fileLength > 0) {
            response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileLength));
        }
    }

    public static String addHttpSchema(String uri, boolean secure) {
        if (Objects.isNull(uri))
            return null;
        String schema = secure ? HTTPS_SCHEMA : HTTP_SCHEMA;
        uri = uri.substring(uri.indexOf(SCHEMA_SUFFIX) + 3);
        return schema.concat(SCHEMA_SUFFIX).concat(uri);
    }

    public static String removeHttpSchema(String uri) {
        if (Objects.isNull(uri))
            return null;
        String httpPrefix = HTTP_SCHEMA + SCHEMA_SUFFIX;
        String httpsPrefix = HTTPS_SCHEMA + SCHEMA_SUFFIX;
        if (uri.startsWith(httpPrefix))
            return uri.substring(httpPrefix.length());
        if (uri.startsWith(httpsPrefix))
            return uri.substring(httpsPrefix.length());
        return uri;
    }

    public static String removeSchema(String uri) {
        if (Objects.isNull(uri))
            return null;
        int i = uri.indexOf(SCHEMA_SUFFIX);
        if (i != -1) {
            uri = uri.substring(uri.indexOf(SCHEMA_SUFFIX) + 3);
        }
        return uri;
    }
}
